/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package eldermodone;

import Hardware_Accelerated.AccelGame;
import java.io.File;
import java.util.ArrayList;
import javax.swing.AbstractListModel;
import javax.swing.ButtonModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;

/**
 *
 * @author KyleSweeney
 */
public class Starter extends javax.swing.JFrame {

    //user defined variables
    ModListModel modListModel;
    AccelGame gameframe;
    MainGame maingame;
    
    public Starter(AccelGame gameFrame, MainGame maingame){
        this();
        this.gameframe=gameFrame;
        this.maingame=maingame;
    }
    
    /**
     * Creates new form Starter
     */
    public Starter() {
        initComponents();
        
        GraphicsRadioGroup.add(Direct3DRadio);
        GraphicsRadioGroup.add(OpenGLRadio);
        FullScreenWindowGroup.add(FullscreenRadio);
        FullScreenWindowGroup.add(WindowedRadio);
        QualityGroup.add(LowRadio); //320x200
        QualityGroup.add(MediumRadio);//640x480
        QualityGroup.add(HighRadio);//800x600
        QualityGroup.add(VeryHighRadio);//1360x768
        QualityGroup.add(UltraRadio);//1680,1050
        
        modListModel = new ModListModel();
        
        if(System.getProperty("os.name").contains("win") || System.getProperty("os.name").contains("Win")){
            GraphicsRadioGroup.setSelected(Direct3DRadio.getModel(), true);
        }else{
            GraphicsRadioGroup.setSelected(OpenGLRadio.getModel(), true);
        }
        
        FullScreenWindowGroup.setSelected(WindowedRadio.getModel(), true);
        QualityGroup.setSelected(HighRadio.getModel(), true);
        ModList.setModel(modListModel);
        
        jComboBox1.setModel(new DefaultComboBoxModel(new String[]{"320x200","640x480","800x600","1024x768","1280x800","1360x768","1440x900","1280x1024","1400x1050","1680x1050","1600x1200","1920x1200"}));
        
        
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        GraphicsRadioGroup = new javax.swing.ButtonGroup();
        FullScreenWindowGroup = new javax.swing.ButtonGroup();
        QualityGroup = new javax.swing.ButtonGroup();
        StartButton = new javax.swing.JButton();
        jInternalFrame2 = new javax.swing.JInternalFrame();
        Direct3DRadio = new javax.swing.JRadioButton();
        OpenGLRadio = new javax.swing.JRadioButton();
        jComboBox1 = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JSeparator();
        WindowedRadio = new javax.swing.JRadioButton();
        FullscreenRadio = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        LowRadio = new javax.swing.JRadioButton();
        MediumRadio = new javax.swing.JRadioButton();
        HighRadio = new javax.swing.JRadioButton();
        VeryHighRadio = new javax.swing.JRadioButton();
        UltraRadio = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        jInternalFrame3 = new javax.swing.JInternalFrame();
        jScrollPane1 = new javax.swing.JScrollPane();
        ModList = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("ElderModOne Manager");
        setResizable(false);

        StartButton.setText("Start!");
        StartButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StartButtonActionPerformed(evt);
            }
        });

        jInternalFrame2.setTitle("Graphics");
        try {
            jInternalFrame2.setSelected(true);
        } catch (java.beans.PropertyVetoException e1) {
            e1.printStackTrace();
        }
        jInternalFrame2.setVisible(true);

        Direct3DRadio.setText("Direct 3D");

        OpenGLRadio.setText("OpenGL");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        WindowedRadio.setText("Windowed");

        FullscreenRadio.setText("FullScreen");

        jLabel1.setText("Resolution");

        LowRadio.setText("Low");

        MediumRadio.setText("Medium");

        HighRadio.setText("High");

        VeryHighRadio.setText("Very High");

        UltraRadio.setText("Ultra");

        jLabel2.setText("           Quality");

        javax.swing.GroupLayout jInternalFrame2Layout = new javax.swing.GroupLayout(jInternalFrame2.getContentPane());
        jInternalFrame2.getContentPane().setLayout(jInternalFrame2Layout);
        jInternalFrame2Layout.setHorizontalGroup(
            jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jInternalFrame2Layout.createSequentialGroup()
                .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(FullscreenRadio)
                    .addComponent(WindowedRadio)
                    .addGroup(jInternalFrame2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(Direct3DRadio, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(OpenGLRadio, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jInternalFrame2Layout.createSequentialGroup()
                        .addComponent(jComboBox1, 0, 91, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(jInternalFrame2Layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(16, 16, 16))))
            .addComponent(jSeparator2)
            .addGroup(jInternalFrame2Layout.createSequentialGroup()
                .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jInternalFrame2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(HighRadio)
                        .addGroup(jInternalFrame2Layout.createSequentialGroup()
                            .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(MediumRadio)
                                .addGroup(jInternalFrame2Layout.createSequentialGroup()
                                    .addGap(2, 2, 2)
                                    .addComponent(LowRadio)))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(VeryHighRadio)
                                .addComponent(UltraRadio)))))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jInternalFrame2Layout.setVerticalGroup(
            jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(Direct3DRadio)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(OpenGLRadio)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(WindowedRadio)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(FullscreenRadio)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 5, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jInternalFrame2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(VeryHighRadio)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(jInternalFrame2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(LowRadio)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(MediumRadio)
                    .addComponent(UltraRadio))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(HighRadio))
        );

        jInternalFrame3.setTitle("Mod Manager");
        try {
            jInternalFrame3.setSelected(true);
        } catch (java.beans.PropertyVetoException e1) {
            e1.printStackTrace();
        }
        jInternalFrame3.setVisible(true);

        ModList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(ModList);

        jButton1.setText("Add Another Mod");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jInternalFrame3Layout = new javax.swing.GroupLayout(jInternalFrame3.getContentPane());
        jInternalFrame3.getContentPane().setLayout(jInternalFrame3Layout);
        jInternalFrame3Layout.setHorizontalGroup(
            jInternalFrame3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addGroup(jInternalFrame3Layout.createSequentialGroup()
                .addComponent(jButton1)
                .addGap(0, 65, Short.MAX_VALUE))
        );
        jInternalFrame3Layout.setVerticalGroup(
            jInternalFrame3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame3Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jInternalFrame3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jInternalFrame2))
                    .addComponent(StartButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jInternalFrame2)
                    .addComponent(jInternalFrame3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(StartButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void StartButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StartButtonActionPerformed
        // TODO add your handling code here:
       int w=0,h=0,resolutionx=0,resolutiony=0;
       boolean fullscreen=false;
       
       if(FullScreenWindowGroup.getSelection() == FullscreenRadio.getModel()){
           fullscreen=true;
           System.out.println("FullScreen!");
       }else{
           fullscreen=false;
           System.out.println("Windowed");
       }
       if(GraphicsRadioGroup.getSelection() == OpenGLRadio.getModel()){
           System.setProperty("sun.java2d.opengl","True");
           System.out.println("OpenGL");
       }else{
           System.setProperty("sun.java2d.d3d","True");
           System.out.println("DirectX");
       }
       
       ButtonModel quality = QualityGroup.getSelection();
       if(quality == LowRadio.getModel()){
           w=320;
           h=200;
       }else if(quality == MediumRadio.getModel()){
           w=640;
           h=480;
       }else if(quality == HighRadio.getModel()){
           w=800;
           h=600;
       }else if(quality == VeryHighRadio.getModel()){
           w=1360;
           h=768;
       }else if(quality == UltraRadio.getModel()){
           w=1680;
           h=1050;
       }
       
       String s = (String)jComboBox1.getSelectedItem();
       resolutionx = Integer.parseInt(s.substring(0,s.indexOf('x')));
       resolutiony = Integer.parseInt(s.substring(s.indexOf("x")+1));
       
        System.out.println("resx: "+resolutionx);
        System.out.println("resy: "+resolutiony);
       
       maingame.setCamera(w, h, resolutionx, resolutiony, fullscreen);
       
       
       maingame.runMods(modListModel.getmods());
       AccelGame.frame.setVisible(true);
       
       gameframe.run();
        
       this.setVisible(false);
    }//GEN-LAST:event_StartButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int choice = chooser.showOpenDialog(this);
        if(choice == JFileChooser.APPROVE_OPTION){
            modListModel.addMod(chooser.getSelectedFile());
            ModList.updateUI();
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Starter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Starter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Starter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Starter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Starter().setVisible(true);
            }
        });
    }
    
    private class ModListModel extends AbstractListModel {

//        File[] modPackages = ElderModOne.getModPackages();
        ArrayList<File> mods;
        
        public ModListModel(){
            String[] properOrder = ElderModOne.getModListFile();
            mods = new ArrayList<File>();
//            for(File f : modPackages){
//                mods.add(f);
//            }
            if (properOrder != null) {

                for (String modPackage : properOrder) {
                    mods.add(new File(modPackage));
                }
            }else{
                for(File modPackage : ElderModOne.getModPackages()){
                    mods.add(modPackage);
                }
            }
        }
        
        public void addMod(String s){
            mods.add(new File(s));
        }
        public void addMod(File f){
            mods.add(f);
        }
        public void addMods(File[] f){
            for(File file : f){
                mods.add(file);
            }
        }
        
        public File[] getmods(){
            File[] f =new File[mods.size()];
            f = mods.toArray(f);
            return f;
        }

        @Override
        public int getSize() {
            return mods.size();
        }

        @Override
        public Object getElementAt(int index) {
            String ret = mods.get(index).getName();
            return ret;
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton Direct3DRadio;
    private javax.swing.ButtonGroup FullScreenWindowGroup;
    private javax.swing.JRadioButton FullscreenRadio;
    private javax.swing.ButtonGroup GraphicsRadioGroup;
    private javax.swing.JRadioButton HighRadio;
    private javax.swing.JRadioButton LowRadio;
    private javax.swing.JRadioButton MediumRadio;
    private javax.swing.JList ModList;
    private javax.swing.JRadioButton OpenGLRadio;
    private javax.swing.ButtonGroup QualityGroup;
    private javax.swing.JButton StartButton;
    private javax.swing.JRadioButton UltraRadio;
    private javax.swing.JRadioButton VeryHighRadio;
    private javax.swing.JRadioButton WindowedRadio;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JInternalFrame jInternalFrame2;
    private javax.swing.JInternalFrame jInternalFrame3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    // End of variables declaration//GEN-END:variables
}
